package com.yeyks.rocketmq.service.customer;

import com.yeyks.commonReference.service.account.AccountInfoService;
import com.yeyks.commonReference.service.order.OrderHeadService;
import com.yeyks.order.dal.domain.OrderHead;
import com.yeyks.websocket.SendMessageBean;
import com.yeyks.websocket.WebSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shuai
 * @date 2019/10/17 14:33
 */
@Service
@Slf4j
public class CommonDisposeService {
    @Autowired
    private OrderHeadService orderHeadService;
    @Autowired
    private AccountInfoService accountService;
    @Autowired
    private WebSocket webSocket;

    /**
     * 处理b2c下单成功
     *
     * @param orderNo
     */
    public void pushMessage(String orderNo) {
        try {
            log.info("b2c下单成功监听到的message消息为{}", orderNo);
            OrderHead orderHead = orderHeadService.getByOrderNo(orderNo);
            if (null != orderHead) {
                List<Integer> accountIds = accountService.selectParentIdByHotelId(orderHead.getHotelId());
                for (Integer accountId : accountIds) {
                    //给子账户推送消息
                    log.info("b2c下单成功监听到给子账户{}推送消息", accountId);
                    webSocket.sendMessageToUser(accountId, SendMessageBean.getOrderMsg(orderNo, orderHead.getVipFlag()));
                    //通过账户id查询出来manageId为该账户id的账户
                    List<Integer> childIds = accountService.selectChildByManageId(accountId);
                    if (null != childIds) {
                        for (Integer childId : childIds) {
                            //给该账户对应的子账户推送新订单通知
                            webSocket.sendMessageToUser(childId, SendMessageBean.getOrderMsg(orderNo, orderHead.getVipFlag()));
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("b2c下单成功消息发送异常{}", orderNo);
        }
    }
}
